﻿using System;
using UFIDA.U9.CBO.Enums;
using UFIDA.U9.CBO.MFG.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.MO.MO;
using UFIDA.U9.PR.PurchaseRequest;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.PL;

namespace UFIDA.U9.CusDev.IFKPlugInBE
{
    // Token: 0x0200000A RID: 10
    internal class AFKPRTOMOPickExtend : IEventSubscriber
    {
        // Token: 0x06000013 RID: 19 RVA: 0x00003BA4 File Offset: 0x00001DA4
        public void Notify(params object[] args)
        {
            
            bool flag = args == null || args.Length == 0 || !(args[0] is EntityEvent);
            if (!flag)
            {
                BusinessEntity.EntityKey key = ((EntityEvent)args[0]).EntityKey;
                bool flag2 = key == null || key.GetEntity() == null;
                if (!flag2)
                {
                    PR.PurchaseRequest.PR pR = key.GetEntity() as PR.PurchaseRequest.PR;
                    bool flag3 = pR == null;
                    if (!flag3)
                    {
                        foreach (PRLine pRLine in pR.PRLineList)
                        {
                            UFIDA.U9.MO.MO.MO mO = UFIDA.U9.MO.MO.MO.Finder.Find("DocNo='" + pRLine.DescFlexSegments.PrivateDescSeg2 + "'", new OqlParam[0]);
                            bool flag4 = mO == null;
                            if (!flag4)
                            {
                                pRLine.DescFlexSegments.PrivateDescSeg3 = mO.Project.Name;
                                pRLine.Project = mO.Project;
                                pRLine.Project.ID = mO.Project.ID;
                                pRLine.Project.Code = mO.Project.Code;
                                pRLine.Project.Name = mO.Project.Name;
                            }
                        }
                        bool flag5 = pR.OriginalData.Status == PRStatusEnum.Approved && pR.Status == PRStatusEnum.OpenOpen && (pR.DocType.Code == "PR3" || pR.DocType.Code == "PR1" || pR.DocType.Code == "PR7");
                        if (flag5)
                        {
                            foreach (PRLine pRLine2 in pR.PRLineList)
                            {
                                pRLine2.DescFlexSegments.PrivateDescSeg1 = "";
                            }
                        }
                        bool flag6 = pR.OriginalData.Status == PRStatusEnum.Approving && pR.Status == PRStatusEnum.Approved && (pR.DocType.Code == "PR3" || pR.DocType.Code == "PR1" || pR.DocType.Code == "PR7");
                        if (flag6)
                        {
                            using (Session s = Session.Open())
                            {
                                int lineNum = 10;
                                foreach (PRLine pRLine3 in pR.PRLineList)
                                {
                                    UFIDA.U9.MO.MO.MO mO2 = UFIDA.U9.MO.MO.MO.Finder.Find("DocNo='" + pRLine3.DescFlexSegments.PrivateDescSeg2 + "'", new OqlParam[0]);
                                    bool flag7 = mO2 == null;
                                    if (!flag7)
                                    {
                                        bool flag8 = mO2.MOPickLists == null;
                                        if (flag8)
                                        {
                                            MOPickList mOPickList = MOPickList.Create(mO2);
                                            mOPickList.DocLineNO = lineNum;
                                            mOPickList.ItemMaster = pRLine3.ItemInfo.ItemID;
                                            mOPickList.ItemMaster.ID = pRLine3.ItemInfo.ItemID.ID;
                                            mOPickList.ItemMaster.Code = pRLine3.ItemInfo.ItemID.Code;
                                            mOPickList.ItemMaster.Name = pRLine3.ItemInfo.ItemID.Name;
                                            // mOPickList.ActualReqQty = pRLine3.ReqQtyTU;
                                            mOPickList.ActualReqQty = decimal.Parse(pRLine3.DescFlexSegments.PrivateDescSeg9);
                                            mOPickList.ActualReqDate = DateTime.Now;
                                            mOPickList.DescFlexField.PrivateDescSeg1 = "True";
                                            mOPickList.OperationNum = "10";
                                            mOPickList.DescFlexField.PrivateDescSeg2 = pRLine3.ID.ToString();
                                            mOPickList.DescFlexField.PrivateDescSeg3 = pR.DocNo;
                                            mOPickList.DescFlexField.PrivateDescSeg4 = pRLine3.DocLineNo.ToString();
                                            mOPickList.IsCalcCost = true;
                                            mOPickList.QtyType = UsageQuantityTypeEnum.Variable;// 数量类型=等于变动
                                             if( pR.DocType.Code == "PR7") {
                                                 mOPickList.IssueStyle = IssueStyleEnum.Push; //发料方式是“推式”
                                            }
                                            bool flag9 = pR.DocType.Code == "PR3";
                                            if (flag9)
                                            {
                                                mOPickList.IssueStyle = IssueStyleEnum.AssemblyPull;
                                            }
                                            else
                                            {
                                                mOPickList.IssueStyle = IssueStyleEnum.Push;
                                            }
                                            lineNum += 10;
                                        }
                                        else
                                        {
                                            foreach (MOPickList item in mO2.MOPickLists)
                                            {
                                                bool flag10 = item.DescFlexField.PrivateDescSeg2 == pRLine3.ID.ToString();
                                                if (flag10)
                                                {
                                                }
                                            }
                                            lineNum = (mO2.MOPickLists.Count + 1) * 10;
                                            MOPickList mOPickList = MOPickList.Create(mO2);
                                            mOPickList.DocLineNO = lineNum;
                                            mOPickList.ItemMaster = pRLine3.ItemInfo.ItemID;
                                            mOPickList.ItemMaster.ID = pRLine3.ItemInfo.ItemID.ID;
                                            mOPickList.ItemMaster.Code = pRLine3.ItemInfo.ItemID.Code;
                                            mOPickList.ItemMaster.Name = pRLine3.ItemInfo.ItemID.Name;
                                           // mOPickList.ActualReqQty = pRLine3.ReqQtyTU;
                                            mOPickList.ActualReqQty = decimal.Parse(pRLine3.DescFlexSegments.PrivateDescSeg9);
                                            mOPickList.ActualReqDate = DateTime.Now;
                                            mOPickList.DescFlexField.PrivateDescSeg1 = "True";
                                            mOPickList.OperationNum = "10";
                                            mOPickList.DescFlexField.PrivateDescSeg2 = pRLine3.ID.ToString();
                                            mOPickList.DescFlexField.PrivateDescSeg3 = pR.DocNo;
                                            mOPickList.DescFlexField.PrivateDescSeg4 = pRLine3.DocLineNo.ToString();
                                            mOPickList.IsCalcCost = true;
                                            if (pR.DocType.Code == "PR7")
                                            {
                                                mOPickList.IssueStyle = IssueStyleEnum.Push; //发料方式是“推式”
                                            }
                                            bool flag11 = pR.DocType.Code == "PR3";
                                            if (flag11)
                                            {
                                                mOPickList.IssueStyle = IssueStyleEnum.AssemblyPull;
                                            }
                                            else
                                            {
                                                mOPickList.IssueStyle = IssueStyleEnum.Push;
                                            }
                                            lineNum += 10;
                                        }
                                    }
                                }
                                s.Commit();
                            }
                        }
                        bool flag12 = pR.OriginalData.Status == PRStatusEnum.Approved && pR.Status == PRStatusEnum.OpenOpen && (pR.DocType.Code == "PR3" || pR.DocType.Code == "PR1" || pR.DocType.Code == "PR7");
                        if (flag12)
                        {
                            using (Session s2 = Session.Open())
                            {
                                foreach (PRLine pRLine4 in pR.PRLineList)
                                {
                                    UFIDA.U9.MO.MO.MO mO3 = UFIDA.U9.MO.MO.MO.Finder.Find("DocNo='" + pRLine4.DescFlexSegments.PrivateDescSeg2 + "'", new OqlParam[0]);
                                    bool flag13 = mO3 == null;
                                    if (!flag13)
                                    {
                                        MOPickList mOPickList = MOPickList.Finder.Find(string.Concat(new string[]
                                        {
                                            "MO=",
                                            mO3.ID.ToString(),
                                            " and DescFlexField.PrivateDescSeg2='",
                                            pRLine4.ID.ToString(),
                                            "'"
                                        }), new OqlParam[0]);
                                        bool flag14 = mOPickList != null;
                                        if (flag14)
                                        {
                                            mOPickList.Remove();
                                        }
                                    }
                                }
                                s2.Commit();
                            }
                        }
                        bool flag15 = pR.OriginalData.Status == PRStatusEnum.Approved && pR.Status == PRStatusEnum.ShortageClose && (pR.DocType.Code == "PR3" || pR.DocType.Code == "PR1" || pR.DocType.Code == "PR7");
                        if (flag15)
                        {
                            using (Session s3 = Session.Open())
                            {
                                foreach (PRLine pRLine5 in pR.PRLineList)
                                {
                                    UFIDA.U9.MO.MO.MO mO4 = UFIDA.U9.MO.MO.MO.Finder.Find("DocNo='" + pRLine5.DescFlexSegments.PrivateDescSeg2 + "'", new OqlParam[0]);
                                    bool flag16 = mO4 == null;
                                    if (!flag16)
                                    {
                                        MOPickList mOPickList = MOPickList.Finder.Find(string.Concat(new string[]
                                        {
                                            "MO=",
                                            mO4.ID.ToString(),
                                            " and DescFlexField.PrivateDescSeg2='",
                                            pRLine5.ID.ToString(),
                                            "'"
                                        }), new OqlParam[0]);
                                        bool flag17 = mOPickList != null;
                                        if (flag17)
                                        {
                                            mOPickList.Remove();
                                        }
                                    }
                                }
                                s3.Commit();
                            }
                        }
                    }
                }
            }
        }
    }
}
